<?php
declare(strict_types=1);

namespace Jxzsxsp\WechatRobot;

use GuzzleHttp\Client;

abstract class Message
{
    protected $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%s';

    public function __construct($key)
    {
        $this->url = sprintf($this->url, $key);
    }

    /**
     * 每个机器人发送的消息不能超过20条/分钟。
     * @see https://work.weixin.qq.com/api/doc/90000/90136/91770
     * @return mixed
     */
    public function send()
    {
        $client = new Client();

        $response = $client->post(
            $this->url,
            [
                'json' => $this->getMessage(),
                'headers' => ['timeout' => 5.0],
                'http_errors' => false,
                'verify' => false
            ]
        );
        $body = $response->getBody()->getContents();
        return json_decode($body, true);
    }

    abstract protected function getMessage();
}